home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / TEXT2GIF.C < prev    next >
C/C++ Source or Header  |  1991-05-12  |  17KB  |  358 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to generate GIF image page from a given text by drawing the chars  *
  7. * using 8 by 8 fixed font.                             *
  8. * Options:                                     *
  9. * -q : quite printing mode.                             *
  10. * -s ColorMapSize : in bits, i.e. 6 bits for 64 colors.                 *
  11. * -f ForeGroundIndex : by default foreground is 1. Must be in range 0..255.  *
  12. * -c R G B : set the foregound color values. By default it is white.         *
  13. * -t "Text" : Make one line given file (8 pixel high) from the given Text.   *
  14. * -h : on line help.                                 *
  15. ******************************************************************************
  16. * History:                                     *
  17. * 3 May 90 - Version 1.0 by Gershon Elber.                     *
  18. *****************************************************************************/
  19.  
  20. #ifdef __MSDOS__
  21. #include <stdlib.h>
  22. #include <alloc.h>
  23. #endif /* __MSDOS__ */
  24.  
  25. #include <stdio.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include "gif_lib.h"
  29. #include "getarg.h"
  30.  
  31. #define PROGRAM_NAME    "Text2Gif"
  32.  
  33. #define MAX_NUM_TEXT_LINES    100     /* Maximum number of lines in file. */
  34.  
  35. #define LINE_LEN        256     /* Maximum length of one text line. */
  36.  
  37. #define DEFAULT_FG_INDEX    1           /* Text foreground index. */
  38.  
  39. #define DEFAULT_COLOR_RED    255           /* Text foreground color. */
  40. #define DEFAULT_COLOR_GREEN    255
  41. #define DEFAULT_COLOR_BLUE    255
  42.  
  43. #ifdef __MSDOS__
  44. extern unsigned int
  45.     _stklen = 16384;                 /* Increase default stack size. */
  46. #endif /* __MSDOS__ */
  47.  
  48. #ifdef SYSV
  49. static char *VersionStr =
  50.         "Gif library module    \t\tGershon Elber\n\
  51.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  52. static char
  53.     *CtrlStr = "Text2Gif q%- s%-ClrMapSize!d f%-FGClr!d c%-R|G|B!d!d!d t%-\"Text\"!s h%-";
  54. #else
  55. static char
  56.     *VersionStr =
  57.     PROGRAM_NAME
  58.     GIF_LIB_VERSION
  59.     "    Gershon Elber,    "
  60.     __DATE__ ",   " __TIME__ "\n"
  61.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  62. static char
  63.     *CtrlStr =
  64.     PROGRAM_NAME
  65.     " q%- s%-ClrMapSize!d f%-FGClr!d c%-R|G|B!d!d!d t%-\"Text\"!s h%-";
  66. #endif /* SYSV */
  67.  
  68. static unsigned int
  69.     RedColor = DEFAULT_COLOR_RED,
  70.     GreenColor = DEFAULT_COLOR_GREEN,
  71.     BlueColor = DEFAULT_COLOR_BLUE;
  72.  
  73. /*****************************************************************************
  74. * Ascii 8 by 8 regular font - only first 128 characters are supported.         *
  75. *****************************************************************************/
  76. static unsigned char AsciiTable[][8] = {
  77.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* Ascii 0  */
  78.     { 0x3c, 0x42, 0xa5, 0x81, 0xbd, 0x42, 0x3c, 0x00 }, /* Ascii 1  */
  79.     { 0x3c, 0x7e, 0xdb, 0xff, 0xc3, 0x7e, 0x3c, 0x00 }, /* Ascii 2  */
  80.     { 0x00, 0xee, 0xfe, 0xfe, 0x7c, 0x38, 0x10, 0x00 }, /* Ascii 3  */
  81.     { 0x10, 0x38, 0x7c, 0xfe, 0x7c, 0x38, 0x10, 0x00 }, /* Ascii 4  */
  82.     { 0x00, 0x3c, 0x18, 0xff, 0xff, 0x08, 0x18, 0x00 }, /* Ascii 5  */
  83.     { 0x10, 0x38, 0x7c, 0xfe, 0xfe, 0x10, 0x38, 0x00 }, /* Ascii 6  */
  84.     { 0x00, 0x00, 0x18, 0x3c, 0x18, 0x00, 0x00, 0x00 }, /* Ascii 7  */
  85.     { 0xff, 0xff, 0xe7, 0xc3, 0xe7, 0xff, 0xff, 0xff }, /* Ascii 8  */
  86.     { 0x00, 0x3c, 0x42, 0x81, 0x81, 0x42, 0x3c, 0x00 }, /* Ascii 9  */
  87.     { 0xff, 0xc3, 0xbd, 0x7e, 0x7e, 0xbd, 0xc3, 0xff }, /* Ascii 10 */
  88.     { 0x1f, 0x07, 0x0d, 0x7c, 0xc6, 0xc6, 0x7c, 0x00 }, /* Ascii 11 */
  89.     { 0x00, 0x7e, 0xc3, 0xc3, 0x7e, 0x18, 0x7e, 0x18 }, /* Ascii 12 */
  90.     { 0x04, 0x06, 0x07, 0x04, 0x04, 0xfc, 0xf8, 0x00 }, /* Ascii 13 */
  91.     { 0x0c, 0x0a, 0x0d, 0x0b, 0xf9, 0xf9, 0x1f, 0x1f }, /* Ascii 14 */
  92.     { 0x00, 0x92, 0x7c, 0x44, 0xc6, 0x7c, 0x92, 0x00 }, /* Ascii 15 */
  93.     { 0x00, 0x00, 0x60, 0x78, 0x7e, 0x78, 0x60, 0x00 }, /* Ascii 16 */
  94.     { 0x00, 0x00, 0x06, 0x1e, 0x7e, 0x1e, 0x06, 0x00 }, /* Ascii 17 */
  95.     { 0x18, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x7e, 0x18 }, /* Ascii 18 */
  96.     { 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x00 }, /* Ascii 19 */
  97.     { 0xff, 0xb6, 0x76, 0x36, 0x36, 0x36, 0x36, 0x00 }, /* Ascii 20 */
  98.     { 0x7e, 0xc1, 0xdc, 0x22, 0x22, 0x1f, 0x83, 0x7e }, /* Ascii 21 */
  99.     { 0x00, 0x00, 0x00, 0x7e, 0x7e, 0x00, 0x00, 0x00 }, /* Ascii 22 */
  100.     { 0x18, 0x7e, 0x18, 0x18, 0x7e, 0x18, 0x00, 0xff }, /* Ascii 23 */
  101.     { 0x18, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00 }, /* Ascii 24 */
  102.     { 0x18, 0x18, 0x18, 0x18, 0x18, 0x7e, 0x18, 0x00 }, /* Ascii 25 */
  103.     { 0x00, 0x04, 0x06, 0xff, 0x06, 0x04, 0x00, 0x00 }, /* Ascii 26 */
  104.     { 0x00, 0x20, 0x60, 0xff, 0x60, 0x20, 0x00, 0x00 }, /* Ascii 27 */
  105.     { 0x00, 0x00, 0x00, 0xc0, 0xc0, 0xc0, 0xff, 0x00 }, /* Ascii 28 */
  106.     { 0x00, 0x24, 0x66, 0xff, 0x66, 0x24, 0x00, 0x00 }, /* Ascii 29 */
  107.     { 0x00, 0x00, 0x10, 0x38, 0x7c, 0xfe, 0x00, 0x00 }, /* Ascii 30 */
  108.     { 0x00, 0x00, 0x00, 0xfe, 0x7c, 0x38, 0x10, 0x00 }, /* Ascii 31 */
  109.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /*   */
  110.     { 0x30, 0x30, 0x30, 0x30, 0x30, 0x00, 0x30, 0x00 }, /* ! */
  111.     { 0x66, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* " */
  112.     { 0x6c, 0x6c, 0xfe, 0x6c, 0xfe, 0x6c, 0x6c, 0x00 }, /* # */
  113.     { 0x10, 0x7c, 0xd2, 0x7c, 0x86, 0x7c, 0x10, 0x00 }, /* $ */
  114.     { 0xf0, 0x96, 0xfc, 0x18, 0x3e, 0x72, 0xde, 0x00 }, /* % */
  115.     { 0x30, 0x48, 0x30, 0x78, 0xce, 0xcc, 0x78, 0x00 }, /* & */
  116.     { 0x0c, 0x0c, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* ' */
  117.     { 0x10, 0x60, 0xc0, 0xc0, 0xc0, 0x60, 0x10, 0x00 }, /* ( */
  118.     { 0x10, 0x0c, 0x06, 0x06, 0x06, 0x0c, 0x10, 0x00 }, /* ) */
  119.     { 0x00, 0x54, 0x38, 0xfe, 0x38, 0x54, 0x00, 0x00 }, /* * */
  120.     { 0x00, 0x18, 0x18, 0x7e, 0x18, 0x18, 0x00, 0x00 }, /* + */
  121.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x70 }, /* , */
  122.     { 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00 }, /* - */
  123.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00 }, /* . */
  124.     { 0x02, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0x00 }, /* / */
  125.     { 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00 }, /* 0x */
  126.     { 0x18, 0x38, 0x78, 0x18, 0x18, 0x18, 0x3c, 0x00 }, /* 1 */
  127.     { 0x7c, 0xc6, 0x06, 0x0c, 0x30, 0x60, 0xfe, 0x00 }, /* 2 */
  128.     { 0x7c, 0xc6, 0x06, 0x3c, 0x06, 0xc6, 0x7c, 0x00 }, /* 3 */
  129.     { 0x0e, 0x1e, 0x36, 0x66, 0xfe, 0x06, 0x06, 0x00 }, /* 4 */
  130.     { 0xfe, 0xc0, 0xc0, 0xfc, 0x06, 0x06, 0xfc, 0x00 }, /* 5 */
  131.     { 0x7c, 0xc6, 0xc0, 0xfc, 0xc6, 0xc6, 0x7c, 0x00 }, /* 6 */
  132.     { 0xfe, 0x06, 0x0c, 0x18, 0x30, 0x60, 0x60, 0x00 }, /* 7 */
  133.     { 0x7c, 0xc6, 0xc6, 0x7c, 0xc6, 0xc6, 0x7c, 0x00 }, /* 8 */
  134.     { 0x7c, 0xc6, 0xc6, 0x7e, 0x06, 0xc6, 0x7c, 0x00 }, /* 9 */
  135.     { 0x00, 0x30, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00 }, /* : */
  136.     { 0x00, 0x30, 0x00, 0x00, 0x00, 0x30, 0x20, 0x00 }, /* }, */
  137.     { 0x00, 0x1c, 0x30, 0x60, 0x30, 0x1c, 0x00, 0x00 }, /* < */
  138.     { 0x00, 0x00, 0x7e, 0x00, 0x7e, 0x00, 0x00, 0x00 }, /* = */
  139.     { 0x00, 0x70, 0x18, 0x0c, 0x18, 0x70, 0x00, 0x00 }, /* > */
  140.     { 0x7c, 0xc6, 0x0c, 0x18, 0x30, 0x00, 0x30, 0x00 }, /* ? */
  141.     { 0x7c, 0x82, 0x9a, 0xaa, 0xaa, 0x9e, 0x7c, 0x00 }, /* @ */
  142.     { 0x38, 0x6c, 0xc6, 0xc6, 0xfe, 0xc6, 0xc6, 0x00 }, /* A */
  143.     { 0xfc, 0xc6, 0xc6, 0xfc, 0xc6, 0xc6, 0xfc, 0x00 }, /* B */
  144.     { 0x7c, 0xc6, 0xc6, 0xc0, 0xc0, 0xc6, 0x7c, 0x00 }, /* C */
  145.     { 0xf8, 0xcc, 0xc6, 0xc6, 0xc6, 0xcc, 0xf8, 0x00 }, /* D */
  146.     { 0xfe, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xfe, 0x00 }, /* E */
  147.     { 0xfe, 0xc0, 0xc0, 0xfc, 0xc0, 0xc0, 0xc0, 0x00 }, /* F */
  148.     { 0x7c, 0xc6, 0xc0, 0xce, 0xc6, 0xc6, 0x7e, 0x00 }, /* G */
  149.     { 0xc6, 0xc6, 0xc6, 0xfe, 0xc6, 0xc6, 0xc6, 0x00 }, /* H */
  150.     { 0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00 }, /* I */
  151.     { 0x1e, 0x06, 0x06, 0x06, 0xc6, 0xc6, 0x7c, 0x00 }, /* J */
  152.     { 0xc6, 0xcc, 0xd8, 0xf0, 0xd8, 0xcc, 0xc6, 0x00 }, /* K */
  153.     { 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0x00 }, /* L */
  154.     { 0xc6, 0xee, 0xfe, 0xd6, 0xc6, 0xc6, 0xc6, 0x00 }, /* M */
  155.     { 0xc6, 0xe6, 0xf6, 0xde, 0xce, 0xc6, 0xc6, 0x00 }, /* N */
  156.     { 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00 }, /* O */
  157.     { 0xfc, 0xc6, 0xc6, 0xfc, 0xc0, 0xc0, 0xc0, 0x00 }, /* P */
  158.     { 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x06 }, /* Q */
  159.     { 0xfc, 0xc6, 0xc6, 0xfc, 0xc6, 0xc6, 0xc6, 0x00 }, /* R */
  160.     { 0x78, 0xcc, 0x60, 0x30, 0x18, 0xcc, 0x78, 0x00 }, /* S */
  161.     { 0xfc, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x00 }, /* T */
  162.     { 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x00 }, /* U */
  163.     { 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x6c, 0x38, 0x00 }, /* V */
  164.     { 0xc6, 0xc6, 0xc6, 0xd6, 0xfe, 0xee, 0xc6, 0x00 }, /* W */
  165.     { 0xc6, 0xc6, 0x6c, 0x38, 0x6c, 0xc6, 0xc6, 0x00 }, /* X */
  166.     { 0xc3, 0xc3, 0x66, 0x3c, 0x18, 0x18, 0x18, 0x00 }, /* Y */
  167.     { 0xfe, 0x0c, 0x18, 0x30, 0x60, 0xc0, 0xfe, 0x00 }, /* Z */
  168.     { 0x3c, 0x30, 0x30, 0x30, 0x30, 0x30, 0x3c, 0x00 }, /* [ */
  169.     { 0xc0, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x03, 0x00 }, /* \ */
  170.     { 0x3c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x3c, 0x00 }, /* ] */
  171.     { 0x00, 0x38, 0x6c, 0xc6, 0x00, 0x00, 0x00, 0x00 }, /* ^ */
  172.     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff }, /* _ */
  173.     { 0x30, 0x30, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* ` */
  174.     { 0x00, 0x00, 0x7c, 0x06, 0x7e, 0xc6, 0x7e, 0x00 }, /* a */
  175.     { 0xc0, 0xc0, 0xfc, 0xc6, 0xc6, 0xe6, 0xdc, 0x00 }, /* b */
  176.     { 0x00, 0x00, 0x7c, 0xc6, 0xc0, 0xc0, 0x7e, 0x00 }, /* c */
  177.     { 0x06, 0x06, 0x7e, 0xc6, 0xc6, 0xce, 0x76, 0x00 }, /* d */
  178.     { 0x00, 0x00, 0x7c, 0xc6, 0xfe, 0xc0, 0x7e, 0x00 }, /* e */
  179.     { 0x1e, 0x30, 0x7c, 0x30, 0x30, 0x30, 0x30, 0x00 }, /* f */
  180.     { 0x00, 0x00, 0x7e, 0xc6, 0xce, 0x76, 0x06, 0x7c }, /* g */
  181.     { 0xc0, 0xc0, 0xfc, 0xc6, 0xc6, 0xc6, 0xc6, 0x00 }, /*  */
  182.     { 0x18, 0x00, 0x38, 0x18, 0x18, 0x18, 0x3c, 0x00 }, /* i */
  183.     { 0x18, 0x00, 0x38, 0x18, 0x18, 0x18, 0x18, 0xf0 }, /* j */
  184.     { 0xc0, 0xc0, 0xcc, 0xd8, 0xf0, 0xd8, 0xcc, 0x00 }, /* k */
  185.     { 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x3c, 0x00 }, /* l */
  186.     { 0x00, 0x00, 0xcc, 0xfe, 0xd6, 0xc6, 0xc6, 0x00 }, /* m */
  187.     { 0x00, 0x00, 0xfc, 0xc6, 0xc6, 0xc6, 0xc6, 0x00 }, /* n */
  188.     { 0x00, 0x00, 0x7c, 0xc6, 0xc6, 0xc6, 0x7c, 0x00 }, /* o */
  189.     { 0x00, 0x00, 0xfc, 0xc6, 0xc6, 0xe6, 0xdc, 0xc0 }, /* p */
  190.     { 0x00, 0x00, 0x7e, 0xc6, 0xc6, 0xce, 0x76, 0x06 }, /* q */
  191.     { 0x00, 0x00, 0x6e, 0x70, 0x60, 0x60, 0x60, 0x00 }, /* r */
  192.     { 0x00, 0x00, 0x7c, 0xc0, 0x7c, 0x06, 0xfc, 0x00 }, /* s */
  193.     { 0x30, 0x30, 0x7c, 0x30, 0x30, 0x30, 0x1c, 0x00 }, /* t */
  194.     { 0x00, 0x00, 0xc6, 0xc6, 0xc6, 0xc6, 0x7e, 0x00 }, /* u */
  195.     { 0x00, 0x00, 0xc6, 0xc6, 0xc6, 0x6c, 0x38, 0x00 }, /* v */
  196.     { 0x00, 0x00, 0xc6, 0xc6, 0xd6, 0xfe, 0x6c, 0x00 }, /* w */
  197.     { 0x00, 0x00, 0xc6, 0x6c, 0x38, 0x6c, 0xc6, 0x00 }, /* x */
  198.     { 0x00, 0x00, 0xc6, 0xc6, 0xce, 0x76, 0x06, 0x7c }, /* y */
  199.     { 0x00, 0x00, 0xfc, 0x18, 0x30, 0x60, 0xfc, 0x00 }, /* z */
  200.     { 0x0e, 0x18, 0x18, 0x70, 0x18, 0x18, 0x0e, 0x00 }, /* { */
  201.     { 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00 }, /* | */
  202.     { 0xe0, 0x30, 0x30, 0x1c, 0x30, 0x30, 0xe0, 0x00 }, /* } */
  203.     { 0x00, 0x00, 0x70, 0x9a, 0x0e, 0x00, 0x00, 0x00 }, /* ~ */
  204.     { 0x00, 0x00, 0x18, 0x3c, 0x66, 0xff, 0x00, 0x00 } /* Ascii 127 */
  205. };
  206.  
  207. static void QuitGifError(GifFileType *GifFile);
  208. static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
  209.                     int BufferWidth, int ForeGroundIndex);
  210.  
  211. /******************************************************************************
  212. * Interpret the command line and generate the given GIF file.              *
  213. ******************************************************************************/
  214. void main(int argc, char **argv)
  215. {
  216.     int    i, j, l, Error, ImageWidth, ImageHeight, NumOfLines, LogNumLevels,
  217.     NumLevels, ClrMapSizeFlag = FALSE, ColorMapSize = 1, ColorFlag = FALSE,
  218.     ForeGroundIndex = DEFAULT_FG_INDEX, ForeGroundFlag = FALSE,
  219.     TextLineFlag = FALSE, HelpFlag = FALSE;
  220.     char *TextLines[MAX_NUM_TEXT_LINES], Line[LINE_LEN];
  221.     GifRowType RasterBuffer[8];
  222.     GifColorType *ColorMap;
  223.     GifFileType *GifFile;
  224.  
  225.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  226.         &GifQuitePrint, &ClrMapSizeFlag, &ColorMapSize,
  227.         &ForeGroundFlag, &ForeGroundIndex,
  228.         &ColorFlag, &RedColor, &GreenColor, &BlueColor,
  229.         &TextLineFlag, &TextLines[0],
  230.         &HelpFlag)) != FALSE) {
  231.     GAPrintErrMsg(Error);
  232.     GAPrintHowTo(CtrlStr);
  233.     exit(1);
  234.     }
  235.  
  236.     if (HelpFlag) {
  237.     fprintf(stderr, VersionStr);
  238.     GAPrintHowTo(CtrlStr);
  239.     exit(0);
  240.     }
  241.  
  242.     if (ForeGroundIndex > 255 || ForeGroundIndex < 1)
  243.     GIF_EXIT("Foregound (-f) should be in the range 1..255, aborted.");
  244.  
  245.     if (ColorMapSize > 8 || ColorMapSize < 1)
  246.     GIF_EXIT("ColorMapSize (-s) should be in the range 1..8, aborted.");
  247.  
  248.     if (TextLineFlag) {
  249.     NumOfLines = 1;
  250.     ImageHeight = 8;
  251.     ImageWidth = 8 * strlen(TextLines[0]);
  252.     }
  253.     else {
  254.     NumOfLines = l = 0;
  255.     while (fgets(Line, LINE_LEN - 1, stdin)) {
  256.         for (i = strlen(Line); i > 0 && Line[i-1] <= ' '; i--);
  257.         Line[i] = 0;
  258.         if (l < i) l = i;
  259.         TextLines[NumOfLines++] = strdup(Line);
  260.         if (NumOfLines == MAX_NUM_TEXT_LINES)
  261.         GIF_EXIT("Input file has too many lines, aborted.");
  262.     }
  263.     if (NumOfLines == 0)
  264.         GIF_EXIT("No input text, aborted.");
  265.     ImageHeight = 8 * NumOfLines;
  266.     ImageWidth = 8 * l;
  267.     }
  268.  
  269.     /* Allocate the raster buffer for 8 scan lines (one text line). */
  270.     for (i = 0; i < 8; i++)
  271.     if ((RasterBuffer[i] = (GifRowType) malloc(sizeof(GifPixelType) *
  272.                             ImageWidth)) == NULL)
  273.         GIF_EXIT("Failed to allocate memory required, aborted.");
  274.  
  275.     /* Open stdout for the output file: */
  276.     if ((GifFile = EGifOpenFileHandle(1)) == NULL)
  277.     QuitGifError(GifFile);
  278.  
  279.     /* Dump out screen description with given size and generated color map: */
  280.     for (LogNumLevels = 1, NumLevels = 2;
  281.      NumLevels < ForeGroundIndex;
  282.      LogNumLevels++, NumLevels <<= 1);
  283.     if (NumLevels < (1 << ColorMapSize)) {
  284.         NumLevels = (1 << ColorMapSize);
  285.     LogNumLevels = ColorMapSize;
  286.     }
  287.  
  288.     if ((ColorMap = (GifColorType *) malloc(NumLevels * sizeof(GifColorType)))
  289.     == NULL) GIF_EXIT("Failed to allocate memory required, aborted.");
  290.  
  291.     for (i = 0; i < NumLevels; i++)
  292.     ColorMap[i].Red = ColorMap[i].Green = ColorMap[i].Blue = 0;
  293.     ColorMap[ForeGroundIndex].Red = RedColor;
  294.     ColorMap[ForeGroundIndex].Green = GreenColor;
  295.     ColorMap[ForeGroundIndex].Blue = BlueColor;
  296.  
  297.     if (EGifPutScreenDesc(GifFile,
  298.     ImageWidth, ImageHeight, LogNumLevels, 0, LogNumLevels, ColorMap)
  299.     == GIF_ERROR)
  300.     QuitGifError(GifFile);
  301.  
  302.     /* Dump out the image descriptor: */
  303.     if (EGifPutImageDesc(GifFile,
  304.     0, 0, ImageWidth, ImageHeight, FALSE, LogNumLevels, NULL) == GIF_ERROR)
  305.     QuitGifError(GifFile);
  306.  
  307.     GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]:     ",
  308.             PROGRAM_NAME, GifFile -> ILeft, GifFile -> ITop,
  309.             GifFile -> IWidth, GifFile -> IHeight);
  310.  
  311.     for (i = l = 0; i < NumOfLines; i++) {
  312.     GenRasterTextLine(RasterBuffer, TextLines[i], ImageWidth,
  313.                             ForeGroundIndex);
  314.     for (j = 0; j < 8; j++) {
  315.         if (EGifPutLine(GifFile, RasterBuffer[j], ImageWidth) == GIF_ERROR)
  316.         QuitGifError(GifFile);
  317.         GifQprintf("\b\b\b\b%-4d", l++);
  318.     }
  319.     }
  320.  
  321.     if (EGifCloseFile(GifFile) == GIF_ERROR)
  322.     QuitGifError(GifFile);
  323. }
  324.  
  325. /******************************************************************************
  326. * Close output file (if open), and exit.                      *
  327. ******************************************************************************/
  328. static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
  329.                     int BufferWidth, int ForeGroundIndex)
  330. {
  331.     char c;
  332.     unsigned char Byte, Mask;
  333.     int i, j, k, CharPosX, Len = strlen(TextLine);
  334.  
  335.     for (i = 0; i < BufferWidth; i++)
  336.         for (j = 0; j < 8; j++) RasterBuffer[j][i] = 0;
  337.  
  338.     for (i = CharPosX = 0; i < Len; i++, CharPosX += 8) {
  339.     c = TextLine[i];
  340.     for (j = 0; j < 8; j++) {
  341.         Byte = AsciiTable[c][j];
  342.         for (k = 0, Mask = 128; k < 8; k++, Mask >>= 1)
  343.         if (Byte & Mask)
  344.             RasterBuffer[j][CharPosX + k] = ForeGroundIndex;
  345.     }
  346.     }
  347. }
  348.  
  349. /******************************************************************************
  350. * Close output file (if open), and exit.                      *
  351. ******************************************************************************/
  352. static void QuitGifError(GifFileType *GifFile)
  353. {
  354.     PrintGifError();
  355.     if (GifFile != NULL) EGifCloseFile(GifFile);
  356.     exit(1);
  357. }
  358.